package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.service.CatalogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class getInfoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String alt = req.getParameter("alt");
        CatalogService catalogService = new CatalogService();
        List<Product> productListByCategory = catalogService.getProductListByCategory(alt);

        String res = "Product ID   Name\n";
        for (int i=0;i<productListByCategory.size();i++)
        {
            res+=productListByCategory.get(i).getProductId() +"    "+productListByCategory.get(i).getName()+"\n";
        }

        PrintWriter writer = resp.getWriter();
        resp.setContentType("text/plain");
        writer.write(res);
        writer.flush();
        writer.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
